package com.taotao.manage.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.taotao.common.bean.ItemCatResult;
import com.taotao.manage.service.ItemCatService;

/**
 *	提供给前台taotao-web的商品类目数据的api接口 
 */

/**
 * @Description:  查询全部商品类目信息，并封装为tree结构
 * 		请求路径：api/item/cat GET
 * 		请求参数：callback
 * 		结果响应：ItemCatResult
 * @author:     mingxu
 */
@RequestMapping(value = "api/item/cat")
@Controller
public class ApiItemCatController {

	@Autowired
	private ItemCatService itemCatService;

	/**
	 * @Description: 配置自定义消息转化器实现统一的jsonp的支持，所以Controller不需要做改变
	 * @return
	 * @author mingxu
	 */
	@RequestMapping(value = "all", method = RequestMethod.GET)
	public ResponseEntity<ItemCatResult> queryItemCat() {

		try {
			//查询并封装成前台商品类目所需的三层结构数据
			ItemCatResult itemCatResult = itemCatService.queryAllToTree();
			return ResponseEntity.status(HttpStatus.OK).body(itemCatResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}

	/**
	 *	手动配置提供jsonp跨域的支持 
	 */
	//@RequestMapping(value = "all", method = RequestMethod.GET)
	/*public ResponseEntity<String> queryItemCat(
			@RequestParam(value = "callback", required = false) String callback) {//提供jsonp跨域的支持
	
		try {
			//查询并封装成前台商品类目所需的三层结构数据
			ItemCatResult itemCatResult = itemCatService.queryAllToTree();
			// PropertiesService中的Mapper将结果转为JSON
			String jsonResult = PropertiesService.MAPPER.writeValueAsString(itemCatResult);
			// 判断是否是跨域请求,如果是，则添加跨域回调函数
			if (StringUtils.isNotEmpty(callback)) {
				System.out.println("================");
				return ResponseEntity.ok(callback + "(" + jsonResult + ")");
			}
			// 如果不是，直接返回结果和成功状态码
			return ResponseEntity.ok(jsonResult);
	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}*/

}
